package com.jiukuaidao.zhulin.module.order.presenter;

import android.widget.Toast;

import com.jiukuaidao.library.Config;
import com.jiukuaidao.library.net.MyRetroCallBack;
import com.jiukuaidao.library.util.ToastUtil;
import com.jiukuaidao.zhulin.api.ApiOrderCancel;
import com.jiukuaidao.zhulin.api.ApiOrderDetail;
import com.jiukuaidao.zhulin.data.TasksRepository;
import com.jiukuaidao.zhulin.entity.ApiOrderDetailInfo;
import com.jiukuaidao.zhulin.module.BasePresenter;
import com.jiukuaidao.zhulin.module.order.contract.OrderDetailContract;

/**
 * Author : BinarySatan
 * Date : 2016/6/3
 * Description :
 */
public class OrderDetailPresenter extends BasePresenter<OrderDetailContract.View, TasksRepository> implements OrderDetailContract.Presenter {

    public OrderDetailPresenter(TasksRepository repository, OrderDetailContract.View view) {
        super(repository, view);
    }

    @Override
    public void start() {
        loadOrderDetailData();
    }


    @Override
    public void loadOrderDetailData() {
        mView.showLoading(null);
        mRepository.loadData(new ApiOrderDetail(mView.getOrderNumber(), new MyRetroCallBack<ApiOrderDetailInfo>() {
            @Override
            public void onSuccess(ApiOrderDetailInfo result, String errorCode, String msg) {
                if (result != null)
                    mView.getOrderDetailDataSuc(result);

                mView.hideLoading();
            }

            @Override
            public void onFail(ApiOrderDetailInfo result, String errorCode, String msg) {
                ToastUtil.show(msg, Toast.LENGTH_SHORT);
                mView.hideLoading();
            }

            @Override
            public void onError(Throwable t, int resCode, String msg) {
                if (Config.Debug)
                    ToastUtil.show("errorCode  :" + resCode + "\n errorMsg :" + msg, Toast.LENGTH_SHORT);

                mView.hideLoading();
            }
        }));

    }

    @Override
    public void orderCancel() {
        mRepository.loadData(new ApiOrderCancel(mView.getOrderNumber(), mView.getOrderCancelReason(), mView.getRsnRemark(), new MyRetroCallBack<String>() {
            @Override
            public void onSuccess(String result, String errorCode, String msg) {
                loadOrderDetailData();
            }

            @Override
            public void onFail(String result, String errorCode, String msg) {
                ToastUtil.show(msg, Toast.LENGTH_SHORT);
            }

            @Override
            public void onError(Throwable t, int resCode, String msg) {
                if (Config.Debug)
                    ToastUtil.show("errorCode  :" + resCode + "\n errorMsg :" + msg, Toast.LENGTH_SHORT);
            }
        }));

    }

}
